<?php
namespace Home\Controller;
use Think\Controller;
class CartController extends Controller{
    public function add(){
        $goods=D('Goods');
        $goods_id=I('get.goods_id')+0;
        if (!($goods->find(I('get.goods_id')+0))){
            $this->redirect('Home/Goods/goods_list');
            exit;
        }
        $cart = \Home\Tool\CartTool::getIns();
        $cart->add($goods->goods_id,$goods->goods_name,$goods->shop_price);
        $msg='加入购物车成功';
        $this->redirect('Home/User/error', array('msg' => $msg),1, '页面跳转中...');
    }
    public function cart_flow(){
        $cart = \Home\Tool\CartTool::getIns();
        $calcMoney=$cart->calcMoney();
//        展示购物车页面单类商品的价格
        $this->assign('calcCnt',$cart->calcCnt());
        $this->assign('calcMoney',$calcMoney);
//        展示加到购物车中商品的信息
        $this->assign('items',$cart->items());
//        展示购物车商品的总价格
        $this->assign('money',$cart->calcMoney());
        $this->display();
    }
    //商品数量减1
    public function deleteOne(){
        $goods_id=I('get.goods_id')+0;
        $cart = \Home\Tool\CartTool::getIns();
        $cart->decr($goods_id);
        $this->redirect('Cart/cart_flow');
    }
    //商品数量加1
    public function incOne(){
        $goods_id=I('get.goods_id')+0;
        $cart = \Home\Tool\CartTool::getIns();
        $cart->incNum($goods_id,1);
        $this->redirect('Cart/cart_flow');
    }
    //删除所有的一类商品
    public function deleteOneAll(){
        $goods_id=I('get.goods_id')+0;
        $cart = \Home\Tool\CartTool::getIns();
        $cart->del($goods_id);
        $this->redirect('Cart/cart_flow');
    }
    public function cart_flowt(){
        $cart = \Home\Tool\CartTool::getIns();
        $calcMoney=$cart->calcMoney();
//        展示购物车页面单类商品的价格
        $this->assign('calcCnt',$cart->calcCnt());
        $this->assign('calcMoney',$calcMoney);
//        展示加到购物车中商品的信息
        $this->assign('items',$cart->items());
//        展示购物车商品的总价格
        $this->assign('money',$cart->calcMoney());
        //取出address表中的 收货地址
        $user_name=session('user_name');
        $home_user=D('Home/HomeUser');
        $user=$home_user->where('user_name='.$user_name)->select();
        $user_id=$user[0]['user_id']+0;
        $address=D('Home/Address');
        $all_address=$address->where('user_id='.$user_id)->select();
        $this->assign('alladdress',$all_address);
        $this->display();
    }
    public function done() {
        $oi = M('Ordinfo');
        $og = M('Ordgoods');
        $cart = \Home\Tool\CartTool::getIns();
    // 写入ordinfo表
        $user_name=session('user_name');
        $home_user=D('Home/HomeUser');
        $user=$home_user->where('user_name='.$user_name)->select();
        $user_id=$user[0]['user_id']+0;
        $address=D('Home/Address');
        $all_address=$address->where('user_id='.$user_id)->select();
//        show_bug($all_address);
        $_POST['address']=$all_address[0]['address'];
        $_POST['xm']=$all_address[0]['name'];
        $_POST['mobile']=$all_address[0]['mobile'];
        $_POST['ord_sn'] = date('Ymd') . rand(1000,9999);
        $_POST['user_id'] = $user_id;
        $_POST['money'] = $cart->calcMoney();
        $_POST['ordtime'] = time();
//        show_bug($_POST);
//        $ordinfo_id = NULL;
        $oi->create($_POST);
        $ordinfo=$oi->add();
        if(!$ordinfo) {
            $this->show('下单失败!' , 'utf-8');
            exit;
        }
        //写入ordgoods 表
        $ordinfo_id=$_POST['ord_sn'];
        $cart = \Home\Tool\CartTool::getIns();
        foreach ($cart->items() as $k => $v){
            $row = array();
            $row['ordinfo_id'] = $ordinfo_id;
            $row['goods_id'] = $k;
            $row['goods_name'] = $v['goods_name'];
            $row['shop_price'] = $v['shop_price'];
            $row['num'] = $v['num'];
            $date[]=$row;
        }
        //如果写入失败，
        if(!$og->addAll($date)) {
            $oi->where('ord_sn='.$ordinfo_id)->delete();
            $og->where('ordinfo_id='.$ordinfo_id)->delete();
            $this->error('下单失败');
            exit;
        }
        $this->assign('ord_sn' , $ordinfo_id);
        $this->assign('money' , $cart->calcMoney());
        //还没有实现
        $CBPay = new \Home\Pay\CBPay();
        $CBPay->v_oid = $_POST['ord_sn'];
        $CBPay->v_amount = $cart->calcMoney();
        $this->assign('pay' , $CBPay->form());
        //用完购物车类后，清空购物车
        // 清空购物车
        $cart->clear();
        $this->display();
    }
}